package com.dormitory.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.manage.mapper.StudentDormitoryMapper;
import com.dormitory.manage.pojo.StudentDormitory;
import com.dormitory.manage.service.StudentDormitoryService;
import com.dormitory.manage.service.StudentService;
import com.dormitory.manage.vo.DormitoryQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description StudentDormitoryServiceImpl
 * @date 2023/4/7 0007 18:59
 */
@Service
@Slf4j
@Transactional
public class StudentDormitoryServiceImpl extends ServiceImpl<StudentDormitoryMapper, StudentDormitory> implements StudentDormitoryService {
    @Resource
    private StudentService studentService;
    /**
    * @description 根据id获取学生宿舍信息
    * @param studentId
    * @returnType com.dormitory.manage.pojo.StudentDormitory
    * @author Administrator
    * @date  19:04
    */
    @Override
    public StudentDormitory getStudentDormitoryById(long studentId) {
       return this.baseMapper.selectById(studentId);
    }

    /**
    * @description 带条件分页查询学生宿舍列表
    * @param page
     * @param dormitoryQueryVo
    * @returnType com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.dormitory.manage.pojo.StudentDormitory>
    * @author Administrator
    * @date  19:39
    */
    @Override
    public Page<StudentDormitory> getStudentDormitoryList(Page<StudentDormitory> page, DormitoryQueryVo dormitoryQueryVo) {
        QueryWrapper<StudentDormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("dormitory_college_name",dormitoryQueryVo.getDormitoryCollegeName())
                .like("dormitory_build_name",dormitoryQueryVo.getDormitoryBuild())
                .like("dormitory_no",dormitoryQueryVo.getDormitoryNo())
                .orderByAsc("dormitory_build_name");
        Page<StudentDormitory> studentDormitoryPage = this.baseMapper.selectPage(page, queryWrapper);
        // 查询宿舍成员
        List<StudentDormitory> records = studentDormitoryPage.getRecords();
        for (StudentDormitory s: records) {
            List<String> dormitoryMember = this.studentService.getDormitoryMember(s.getDormitoryNo(), s.getDormitoryBuildName());
            s.setDormitoryMember(dormitoryMember);
        }
        return studentDormitoryPage;
    }

    /**
    * @description 新增学生宿舍
    * @param studentDormitory
    * @returnType boolean
    * @author Administrator
    * @date  14:49
    */
    @Override
    public boolean addStudentDormitory(StudentDormitory studentDormitory) {
        QueryWrapper<StudentDormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dormitory_build_name",studentDormitory.getDormitoryBuildName())
                .eq("dormitory_no",studentDormitory.getDormitoryNo());
        StudentDormitory dormitory = this.baseMapper.selectOne(queryWrapper);
        if (dormitory == null){
           return this.baseMapper.insert(studentDormitory) > 0;
        }
        return false;
    }

    /**
    * @description 根据id删除学生宿舍
    * @param dormitoryId
    * @returnType boolean
    * @author Administrator
    * @date  17:11
    */
    @Override
    public boolean deleteStudentDormitory(Integer dormitoryId) {
        StudentDormitory studentDormitory = this.baseMapper.selectById(dormitoryId);
        if (studentDormitory != null){
            return this.baseMapper.deleteById(dormitoryId) > 0;
        }
        return false;
    }


}
